package parsetree;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.DocumentEvent.ElementChange;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;

import metagrammar.Type;

import com.google.common.collect.ImmutableList;

public class RootElement extends AbstractBranchElement implements Verifiable {
    
    public static final String NAME = "root";
    
    public RootElement(ParseTree parseTree) throws BadLocationException
    {
        super(parseTree, null, new ArrayList<Element>());
        LineElement firstLine = new LineElement(this);
        AbstractElement child = new ParsingElement(firstLine, 0, 1);
        firstLine.addChild(child);
        addChild(firstLine);
    }
    
    @Override
    public boolean verify()
    {
        boolean verified = true;
        int length = getChildCount();
        for(int i = 0; i < length; i++) {
            Element child = getElement(i);
            if(child instanceof Verifiable) {
                verified &= ((Verifiable) child).verify();
            }
        }
        return verified;
    }
    
    @Override
    public Type getType()
    {
        return Type.UNTYPED;
    }
    
    @Override
    public String getName()
    {
        return NAME;
    }
    
    @Override
    public AbstractBranchElement getParent()
    {
        return null;
    }
    
    /**
     * Splits the children of this root at the given modelOffet. 
     */
    @Override
    public Element[] split(int modelOffset)
    {
        return null;
    }
    
    @Override
    public Element merge(Element element)
    {
        return null;
    }
    
    @Override
    public List<ElementChange> insertUpdate(Insert change, AttributeSet attributes)
    {
        return insertUpdateChildren(change, attributes);
    }
    
    @Override
    public List<ElementChange> postRemoveUpdate(Removal change)
    {
        return postRemoveUpdateChildren(change);
    }
    
    public LineElement getFirstLine()
    {
        ImmutableList<Element> children = getChildren();
        for(Element child : children) {
            if(child instanceof LineElement) {
                return (LineElement) child;
            }
        }
        return null;
    }
}